//
//  EndViewController.swift
//  MMTest
//
//  Created by liujianlong on 2022/9/20.
//

import UIKit
let defaultEmail = "491825065@qq.com"
let localEmailKey = "localEmailKey"
let localNameKey = "localNameKey"
class EndViewController: EPViewController {
    @IBOutlet var time: UILabel!
    @IBOutlet var input1: UITextField!
    @IBOutlet var input2: UITextField!
    @IBOutlet var enterBtn: UIButton!
    var record = Record()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initUI()
    }
    
    override func initUI() {
        self.title = LS("结束记录")
        self.input2.text = UserDefaults.standard.value(forKey:localNameKey) as? String ?? ""
        self.record.tester = self.input2.text!
        
        self.input1.text = UserDefaults.standard.value(forKey:localEmailKey) as? String ?? ""
        self.record.email = self.input1.text!
        
        let startstamp = NSDate.tranToStamp(self.record.startTime, "yyyy-MM-dd HH:mm:ss").intValue()
        let endtamp = NSDate.tranToStamp(self.record.endTime, "yyyy-MM-dd HH:mm:ss").intValue()
        let t = endtamp - startstamp
        let hour = t / 3600
        let min = t % 3600 / 60
        let sec = t % 3600 % 60
        self.time.text = String(format:"%02d:%02d:%02d", hour,min,sec)
    }
    
    ///返回
    override func backBtnAction() {
        if self.judge(){
            if self.record.data.count > 0 {
                self.alert(LS("提示"), LS("是否不将本次数据发送至邮箱？")) {
                } _: {
                    self.record.noUpload = true
                    self.record.bg_saveOrUpdateAsync { success
                        in }
                    self.navigationController?.popToRootViewController(animated: true)
                }
            }else{
                NetworkTool.deleteLocal(self.record) { success in }
                self.navigationController?.popToRootViewController(animated: true)
            }
        }
    }
    
    @IBAction func actionForBtn(_ sender: UIButton) {
        if self.judge(){
            RLHud.start("发送中...")
            sender.isSelected = true
            UserDefaults.standard.setValue(self.input1.text, forKey: localEmailKey)
            UserDefaults.standard.setValue(self.input2.text, forKey: localNameKey)
            UserDefaults.standard.synchronize()
            
            self.record.email = self.input1.text!
            self.record.tester = self.input2.text!
            NetworkTool.upload(self.record) {
                self.record.data.removeAllObjects()
                DispatchQueue.main.async {
                    self.navigationController?.popToRootViewController(animated: true)
                    self.enterBtn.isSelected = false
                    RLHud.stop()
                }
            }
        }
    }
    
    func judge()->Bool{
        if self.record.tester.count <= 0{
            RLHud.start(LS("请先填写本次测试的名称"), stopTime: 1)
            return false
        }
        if self.record.email.count <= 0{
            self.input1.text = defaultEmail
            return true
        }
        if self.record.email.isEmail() == false{
            RLHud.start(LS("邮箱格式不对"), stopTime: 1)
            return false
        }
        if self.enterBtn.isSelected == true{
            RLHud.start(LS("已经发送，请勿重复发送"), stopTime: 1)
            return false
        }
        if self.record.data.count <= 0{
            RLHud.start(LS("当前没有有效数据"), stopTime: 1) {
                self.navigationController?.popToRootViewController(animated: true)
            };
            return false
        }
        return true
    }
}

//MARK: - UITextFieldDelegate
extension EndViewController:UITextFieldDelegate{
    func textFieldDidChangeSelection(_ textField: UITextField)
    {
        if textField == self.input1{
            self.record.email = textField.text!
        }else if textField == self.input2{
            self.record.tester = textField.text!
        }
    }
}
